// pages/details/details.js
const app = getApp()
var url = require('../../utils/url.js');
var goodsId;
var token;
var user_id = 0;
var formid;
var page = 1;
var last_page = 1;
var goods_id;
var loadMore = function (that) {
  wx.showLoading({
    title: '加载中',
  });
  if (page == 1) {
    page = 2
  }
  if (page < last_page || page == last_page) {
    console.log(page)
    wx.request({
      url: url.URl + 'goods/treasure/log',
      data: { id: goodsId, goods_id: goods_id, page: page },
      method: 'get',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        wx.hideLoading();
        that.setData({
          thisTimeItem: that.data.thisTimeItem.concat(res.data.data.data)
        });
        page++;
      }
    });
  } else {
    wx.hideLoading();
    that.setData({
      nodata: 1,
      skill: true
    })
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banner: [],
    indicatorDots: true,  //小点
    indicatorColor: "white",
    autoplay: true,  //是否自动轮播
    interval: 3000,  //间隔时间
    duration: 500,  //滑动时间
    tabs: ["商品详情", "本次参与"],
    activeIndex: 0,
    thisTimeItem: [],
    row: "",
    image: "",
    num: 1,
    spec: true,
    goods: "",
    shaiLIst: "",
    myVideo: true,
    jifen: "",
    jiang:""
  },
  tabClick: function (e) {
    var that = this;
    var idx = e.currentTarget.dataset.idx;
    var activeIndex = that.data.activeIndex;
    if (idx == activeIndex) {
      return false
    } else {
      that.setData({
        activeIndex: idx
      })
    }
  },
  getJifen() {
    var that = this;
    wx.request({
      url: url.URl + 'my/jifen',
      data: {},
      method: 'get',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        var msg = res.data.msg
        if (res.data.code == 200) {
          that.setData({
            jifen: res.data.data
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  tanSpec: function (e) {
    var that = this;
    token = wx.getStorageSync("token")
    formid = e.detail.formId;
    this.setData({
      formid: formid
    })
    if (token) {
      this.setData({
        spec: false
      })
    } else {
      app.onGotUserInfo(e, that, formid)
    }
  },
  toIndex: function () {
    wx.switchTab({
      url: '/pages/index/index',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  toIndex1: function () {
    wx.navigateBack({
      delta: 1
    })
  },
  close: function () {
    this.setData({
      spec: true
    })
  },
  getGood() {
    var that = this;
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'goods/treasure/info',
      data: { id: goodsId },
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var msg = res.data.msg;
        console.log(res)
        wx.hideLoading()
        if (res.data.code == 200) {
          var goods = res.data.data.goods;
          var billF = (res.data.data.goods_salenum) / (res.data.data.goods.goods_storage) * 100
          var bill = billF.toFixed(2) + "%";
          var video = res.data.data.goods.goods_video;
          if (video == "") {

            console.log(video, 123213122)
            that.setData({
              myVideo: true
            })
          } else {
            that.setData({
              myVideo: false
            })
          }
          that.setData({
            banner: goods.goods_images,
            row: goods,
            bill: bill,
            goods: res.data.data,
            video: goods.goods_video
          })
          goods_id = res.data.data.goods_id;
          console.log(goods_id)
          that.getLog(goodsId, goods_id);
          that.getShaiDan(goods_id);
        } else if (res.data.code == 201) {

        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  close: function () {
    this.setData({
      myVideo: true
    })
  },
  toTreasure: function () {
    var that = this;
    var data = {};
    wx.showModal({
      title: '提示',
      content: '确认使用积分兑换吗',
      showCancel: true,
      cancelText: '取消',
      confirmText: '确定',
      confirmColor: '#DD2626',
      success: function (res) {
        if (res.confirm) {
          var goods = that.data.goods
          data.goods_id = goods.goods_id;
          data.more = that.data.num;
          data.qishu = goods.id;
          console.log(data)
          wx.request({
            url: url.URl + 'goods/treasure/tobuy',
            data: data,
            method: 'post',
            header: {
              'Authorization': token
            },
            success: function (res) {
              console.log(res)
              var msg = res.data.msg;
              if (res.data.code == 200) {
                wx.navigateTo({
                  url: '/pages/success/success',
                  success: function (res) { },
                  fail: function (res) { },
                  complete: function (res) { },
                })
              } else if (res.data.code == 201) {
                wx.showModal({
                  title: '积分不足',
                  content: '购买优惠券获得积分',
                  showCancel: true,
                  confirmColor: '#DD2626',
                  success: function (res) {
                    if (res.confirm) {
                      wx.navigateTo({
                        url: '/pages/coupon/coupon?type=1',
                        success: function (res) { },
                        fail: function (res) { },
                        complete: function (res) { },
                      })
                    } else {
                      that.setData({
                        spec: true
                      })
                    }
                  },
                  fail: function (res) { },
                  complete: function (res) { },
                })
              } else {
                wx.showToast({
                  title: msg,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })

  },
  getLog(goodsId, goods_id) {
    var that = this;
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'goods/treasure/log',
      data: { id: goodsId, goods_id: goods_id, page: page },
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var msg = res.data.msg;
        console.log(res)
        wx.hideLoading()
        if (res.data.code == 200) {
          last_page = res.data.data.last_page
          that.setData({
            thisTimeItem: res.data.data.data
          })
        } else if (res.data.code == 201) {

        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getShaiDan(goods_id) {
    var that = this;
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'goods/treasure/award/show',
      data: { goods_id: goods_id },
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var msg = res.data.msg;
        console.log(res)
        wx.hideLoading()
        if (res.data.code == 200) {
          that.setData({
            shaiLIst: res.data.data
          })
        } else if (res.data.code == 201) {

        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this;
    goodsId = options.goodsId;
    var item=JSON.parse(options.item)
    that.getGood()
    that.setData({
      jiang:item,
      goodsId: goodsId
    })
  },
  imageLoad: function (e) {
    var $width = e.detail.width,    //获取图片真实宽度
      $height = e.detail.height,
      ratio = $width / $height;    //图片的真实宽高比例
    var viewWidth = 750,           //设置图片显示宽度，左右留有16rpx边距
      viewHeight = 750 / ratio;    //计算的高度值
    var image = this.data.row.goods_body;
    //将图片的datadata-index作为image对象的key,然后存储图片的宽高值
    image[e.target.dataset.index] = {
      width: viewWidth,
      height: viewHeight
    }
    // console.log(image)
    this.setData({
      image: image
    })
  },
  bindMinus: function () {
    var num = this.data.num;
    if (num > 1) {
      num--;
    }
    // 将数值与状态写回
    this.setData({
      num: num,
    });
  },
  /* 点击加号 */
  bindPlus: function () {
    var num = this.data.num;
    var goodStorage = this.data.goods.goods_storage;
    // console.log(num)
    // console.log(this.data.goods)
    if (num < goodStorage) {
      num++;
      this.setData({
        num: num,
      });
    } else {
      wx.showToast({
        title: '不能再大了',
        icon: 'none',
      })
    }


    // 将数值与状态写回

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    wx.getStorage({
      key: 'token',
      success: function (res) {
        token = res.data
        that.getJifen()
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
     page=1;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(page)
    var that = this;
    if (that.data.index != 0) {
      loadMore(that)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this;
    var row = that.data.row;
    var goods = that.data.goods
    if (res.from === 'button') {

    }
    return {
      title: row.goods_name,
      path: '/pages/jfDetails/jfDetails?goodsId=' + goods.id,
      imageUrl: row.goods_thumb,
      success: function (res) {
        console.log('成功', res)
      }
    }
  }
})